package com.mes.defece.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mes.common.pojo.MESResult;
import com.mes.defece.mapper.TestingProjectMapper;
import com.mes.defece.service.TestingProjectService;
import com.mes.pojo.device.Device;
import com.mes.pojo.defece.TestingProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * @Author 侯伟豪
 * @Company 起点编程
 * @Description
 * @Time 2024/10/17 16:54
 */
@Service
public class TestingProjectServiceImpl implements TestingProjectService {
    @Autowired
    private TestingProjectMapper testingProjectMapper;
    @Override
    public MESResult selectAll(TestingProject testingProject, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<TestingProject> testingProjects = testingProjectMapper.selectAll(testingProject);
        return MESResult.success(new PageInfo<>(testingProjects));
    }
    @Override
    public MESResult getExport(TestingProject testingProject, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<TestingProject> testingProjects = testingProjectMapper.selectAll(testingProject);
        File folder = new File("D:\\MES\\Excel");
        if (!folder.exists()){
            folder.mkdirs();
        }
        EasyExcel.write("D:\\MES\\Excel\\testingProject-"+ page + limit + ".xlsx", TestingProject.class).sheet("检测项数据").doWrite(testingProjects);
        return MESResult.success(new PageInfo<>(testingProjects));
    }

    @Override
    public MESResult selectByTemplateId(Integer TestingProjectTemplateId) {
        return MESResult.success(testingProjectMapper.selectByTemplateId(TestingProjectTemplateId));
    }

    @Override
    public MESResult addTestingProject(TestingProject testingProject) {
        if (testingProject.getTestingProjectStatus() == null){
            testingProject.setTestingProjectStatus(1);
        }
        testingProject.setTestingProjectTemplateId(1L);
        return MESResult.success(testingProjectMapper.insert(testingProject));
    }

    @Override
    public MESResult selectById(Long testingProjectId) {
        return MESResult.success(testingProjectMapper.selectById(testingProjectId));
    }

    @Override
    public MESResult updateTestingProject(TestingProject testingProject) {
        return MESResult.success(testingProjectMapper.updateDefeceById(testingProject));
    }

    @Override
    public MESResult deleteTestingProject(Long testingProjectId,Integer testingProjectStatus) {
        TestingProject testingProject = new TestingProject();
        testingProject.setTestingProjectId(testingProjectId);
        testingProject.setTestingProjectStatus(99);
        return MESResult.success(testingProjectMapper.updateDefeceStatus(testingProject));
    }
}